package org.mythflow.domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;

public class Recording implements Parcelable {

	private static SimpleDateFormat PARSER = new SimpleDateFormat( "yyyyMMddHHmmss" );
	private static SimpleDateFormat FORMATTER_LONG = new SimpleDateFormat( "yyyy/MM/dd' @ 'hh:mm" );
	private static SimpleDateFormat FORMATTER_SHORT = new SimpleDateFormat( "MM/dd" );

	private String title;
	private String subTitle;
	private String description;
	private Date startTime;
	private Date endTime;
	private String length;
	private String url;
	private String channelId;
	private boolean currentlyRecording;
	
    /**
     * 
     */
    public static final Parcelable.Creator<Recording> CREATOR = new Parcelable.Creator<Recording>() {
    	public Recording createFromParcel( Parcel in ) {
    		return new Recording( in );
		}

		public Recording[] newArray( int size ) {
    		return new Recording[ size ];
		}
    };

    /**
     * 
     */
    public Recording() { }
    
    /**
	 * @param in
	 */
	public Recording( Parcel in ) { 
		readFromParcel( in );
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle( String title ) {
		this.title = title;
	}

	/**
	 * @return the subTitle
	 */
	public String getSubTitle() {
		return subTitle;
	}

	/**
	 * @param subTitle the subTitle to set
	 */
	public void setSubTitle( String subTitle ) {
		this.subTitle = subTitle;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription( String description ) {
		this.description = description;
	}

	/**
	 * @return the startTime
	 */
	public Date getStartTime() {
		return startTime;
	}

	public String getStartTimeAsString() {
		return PARSER.format( startTime );
	}
	
	/**
	 * @return
	 */
	public String getStartTimeFormattedLong() {
		return FORMATTER_LONG.format( startTime );
	}
	
	/**
	 * @return
	 */
	public String getStartTimeFormattedShort() {
		return FORMATTER_SHORT.format( startTime );
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime( Date startTime ) {
		this.startTime = startTime;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime( String startTime ) {
		try {
			setStartTime( PARSER.parse( startTime ) );
		} catch( ParseException e ) {
			setStartTime( new Date() );
		}
	}

	/**
	 * @return the endTime
	 */
	public Date getEndTime() {
		return endTime;
	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime( Date endTime ) {
		this.endTime = endTime;
		
		if( this.endTime.after( new Date() ) ) {
			this.currentlyRecording = true;
		} else {
			this.currentlyRecording = false;
		}
	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime( String endTime ) {
		try {
			setEndTime( PARSER.parse( endTime ) );
		} catch( ParseException e ) {
			setEndTime( new Date() );
		}
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl( String url ) {
		this.url = url;
	}

	/**
	 * @return the currentlyRecording
	 */
	public boolean isCurrentlyRecording() {
		return currentlyRecording;
	}

	/**
	 * @param currentlyRecording the currentlyRecording to set
	 */
	public void setCurrentlyRecording( boolean currentlyRecording ) {
		this.currentlyRecording = currentlyRecording;
	}

	/**
	 * @return the channelId
	 */
	public String getChannelId() {
		return channelId;
	}

	/**
	 * @param channelId the channelId to set
	 */
	public void setChannelId( String channelId ) {
		this.channelId = channelId;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "Recording [title=" ).append( title ).append( ", subTitle=" )
				.append( subTitle ).append( ", description=" ).append( description )
				.append( ", channelId=" ).append( channelId )
				.append( ", startTime=" ).append( startTime ).append( ", endTime=" )
				.append( endTime ).append( ", length=" ).append( length ).append( ", url=" ).append( url )
				.append( ", currentlyRecording=" ).append( currentlyRecording )
				.append( "]" );
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see android.os.Parcelable#describeContents()
	 */
	public int describeContents() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	public void writeToParcel( Parcel dest, int flags ) {
		dest.writeString( title );
		dest.writeString( subTitle );
		dest.writeString( description );
		dest.writeString( channelId );
		dest.writeString( PARSER.format( startTime ) );
		dest.writeString( PARSER.format( endTime ) );
		dest.writeString( url );
		dest.writeString( length );
	}
	
	public void readFromParcel( Parcel in ) {
		title = in.readString();
		subTitle = in.readString();
		description = in.readString();
		channelId = in.readString();
		setStartTime( in.readString() );
		setEndTime( in.readString() );
		url = in.readString();
		length = in.readString();
	}

}
